/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBManager;

import java.sql.DataTruncation;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.RowSet;
import javax.swing.JOptionPane;

/**
 *
 * @author Р�РІР°РЅ
 */
public abstract class DataManager {

    protected Connection connection;
    protected PreparedStatement SelectPrepStatement;
    protected PreparedStatement UpdatePrepStatement;
    protected PreparedStatement InsertPrepStatement;
    protected PreparedStatement DeletePrepStatement;
    protected ResultSet DataSet;
    public final String TableName;
    public  String ID_Table;

    protected DataManager(Connection connection, String TableName) {
        this.connection = connection;
        this.TableName = TableName;
        SelectPrepStatement = null;
        UpdatePrepStatement = null;
        InsertPrepStatement = null;
        DeletePrepStatement = null;
        DataSet = null;

    }

    public void UpdateRow() {
        try {

            connection.setAutoCommit(false);

            SetValues(UpdatePrepStatement);
            UpdatePrepStatement.executeUpdate();

            connection.commit();

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error database access!");

        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null, "Error database access!");
            }
        }

    }

    public void InsertRow() {
        try {

            connection.setAutoCommit(false);

            SetValues(InsertPrepStatement);
            InsertPrepStatement.executeUpdate();

            connection.commit();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error database access!");

        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null, "Error database access!");
            }
        }
    }

    public void DeleteRow() {
        try {

            connection.setAutoCommit(false);

            SetValues(DeletePrepStatement);
            DeletePrepStatement.executeUpdate();

            connection.commit();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error database access!");

        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null, "Error database access!");
            }
        }

    }

    public ResultSet LoadTable() {
        try {

            DataSet = SelectPrepStatement.executeQuery();

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error database access!");
        } finally {
            return DataSet;
        }

    }

    protected abstract void SetValues(PreparedStatement prepareStatement);
}
